package com.googlecode.recycled.lang.assertions;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.apache.commons.lang.exception.ExceptionUtils;

import java.util.List;

public class ThrowablesExt {

    private ThrowablesExt() {
    }

    public static final Function<Throwable, List<String>> F_THROWABLE_SUMMARY = new Function<Throwable, List<String>>() {
        @Override
        public List<String> apply(Throwable throwable) {
            if (throwable == null) {
                return Lists.newLinkedList();
            }
            List<String> messages = Lists.newLinkedList();
            do {
                messages.add(ExceptionUtils.getMessage(throwable));
            } while ((throwable = ExceptionUtils.getCause(throwable)) != null);
            return messages;
        }
    };

    public static List<String> throwableToSummary(Throwable throwable) {
        return F_THROWABLE_SUMMARY.apply(throwable);
    }

}
